package game.network
{
	import engine.asset.*;
	import engine.core.*;
	import game.messages.gamedata.*;
	
	public class RxGameData
	{
		private var m_server:RxTransactionServer;
		private var m_requests:Object;
		private static var s_instance:RxGameData;
		
		public function RxGameData(server:RxTransactionServer)
		{
			this.m_requests = new Object();
			s_instance = this;
			this.m_server = server;
			this.m_server.AddListener(RxGameData_UpdateMessage.messageId, this.OnGameDataUpdate);
		}
		
		private function OnGameDataUpdate(message:RxGameData_UpdateMessage) : void
		{
			var info:String = null;
			var requestCallback:RequestCallback = null;
			var csv:* = RxAssetCSV.GetByName(message.nameSpace);
			if (csv != null)
			{
				csv.AddRowFromJSON(message.name, message.data);
				info = message.nameSpace + "|" + message.name;
				if (this.m_requests.hasOwnProperty(info))
				{
					for each (requestCallback in this.m_requests[info])
					{
						
						requestCallback.callback.apply(null, requestCallback.params);
					}
					delete this.m_requests[info];
				}
			}
			else
			{
				RxLog.Error("received game data message for non-existant asset: " + message.nameSpace);
			}
		}
		
		public function Request(param1:String, param2:String, param3:Function, param4:Array) : Boolean
		{
			var _loc_8:String = null;
			var _loc_9:RxGameData_RequestMessage = null;
			var _loc_5:* = RxAssetCSV.GetByName(param1);
			var _loc_6:Boolean = false;
			var _loc_7:Array = [param1, param2];
			if (param4 != null)
			{
				_loc_7 = _loc_7.concat(param4);
			}
			if (param1 == null || param2 == null || param2.length == 0)
			{
				RxLog.Error("Invalid gamedata request for " + param2 + " in namespace " + param1);
			}
			else if (_loc_5 != null)
			{
				if (_loc_5.RowExists(param2))
				{
					if (param3 != null)
					{
						param3.apply(null, _loc_7);
					}
				}
				else
				{
					_loc_8 = param1 + "|" + param2;
					if (!this.m_requests.hasOwnProperty(_loc_8))
					{
						this.m_requests[_loc_8] = new Vector.<RequestCallback>;
						_loc_9 = new RxGameData_RequestMessage();
						_loc_9.nameSpace = param1;
						_loc_9.name = param2;
						this.m_server.SendMessage(_loc_9);
					}
					if (param3 != null)
					{
						this.m_requests[_loc_8].push(new RequestCallback(param3, _loc_7));
					}
				}
				_loc_6 = true;
			}
			else
			{
				RxLog.Error("received game data request for non-existant asset: " + param1);
			}
			return _loc_6;
		}
		
		public function CreateTable(param1:String) : void
		{
			RxAssetCSV.Create(null, param1);
			var _loc_2:* = new RxGameData_RequestMessage();
			_loc_2.nameSpace = param1;
			_loc_2.name = "";
			this.m_server.SendMessage(_loc_2);
		}
		
		public static function get instance() : RxGameData
		{
			return s_instance;
		}
	}
}

class RequestCallback
{
	public var callback:Function;
	public var params:Array;
	
	public function RequestCallback(callback:Function = null, params:Array = null)
	{
		this.callback = callback;
		this.params = params;
	}	
}